package com.test5.liang.controller;

import com.test5.liang.dao.UserMapper;
import com.test5.liang.pojo.User;
import com.test5.liang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class mainController {

    @Autowired
    UserService userService;

    @RequestMapping("/")
    public String index() {
        return "login";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @ResponseBody
    @RequestMapping("/test")
    public String test() {
        User user = userService.findUserByName("liang");

        return user.toString();
    }

    @RequestMapping("/userIndex/{username}")
    public String userIndex(@PathVariable("username")String username,Model model) {
        model.addAttribute("username",username);
        return "userIndex";
    }

    @ResponseBody
    @RequestMapping("/adminIndex")
    public String adminIndex() {

        return "你好,这里是管理员界面";
    }

     /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 15:59 2021/12/14
     * @Param 注册新用户
     * @return
     **/
    @RequestMapping("/signUp")
    public String signUp(@RequestParam("userName")String userName,
                         @RequestParam("realName")String realName,
                         @RequestParam("password")String password) {
        User user = new User();
        user.setUserName(userName);
        user.setRealName(realName);
        user.setPassword(password);
        //先设置为普通用户类型
        user.setUserRole(1);
        user.setStatus(1);
        userService.insertUser(user);
        return "login";
    }

    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 21:07 2021/12/14
     * @Param 注册查看是否已经存在用户
     * @return java.lang.String
     **/
    @ResponseBody
    @RequestMapping("/checksignUp")
    public String checksignUp(@RequestParam("username")String username) {
        System.out.println("checksignUp------" + username);
        //查看用户是否已经存在，若存在则注册失败
        User user = userService.findUserByName(username);
        if (user != null) {
            //已经存在用户
            return "false";
        }else {
            return "true";
        }
    }


}